#!/usr/bin/env python

import contextlib
import os
import subprocess
import sys


SOURCE_ROOT = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))


def main():
  os.chdir(SOURCE_ROOT)
  result = (subprocess.call(['git', 'submodule', 'sync']) or
            subprocess.call(['git', 'submodule', 'update',
                             '--init', '--recursive']))
  if result:
    return result

  # Setup boto.
  with scoped_cwd(os.path.join('vendor', 'boto')):
    subprocess.call([sys.executable, 'setup.py', 'build'])


@contextlib.contextmanager
def scoped_cwd(path):
  cwd = os.getcwd()
  os.chdir(path)
  try:
    yield
  finally:
    os.chdir(cwd)


if __name__ == '__main__':
  sys.exit(main())
